package com.server.http;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

public class HttpContext {
	private static Map<String, String> type = new HashMap<>();
	
	static{
		parseWebXml();
	}
	
	
	public static String getType(String key) {
		return type.get(key);
	}


	public static void parseWebXml(){
		
		try {
			File file = new File("./web.xml");
			SAXReader reader = new SAXReader();
			Document doc = reader.read(file);
			
			Element ele = doc.getRootElement();
			List<Element> list = ele.elements("mime-mapping");
			
			for(Element e : list){
				String extension = e.elementTextTrim("extension");
				String mime_type = e.elementTextTrim("mime-type");
				type.put(extension, mime_type);
			}
			
			
		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	
}
